<?php

namespace app\oa\controller;

use cmf\controller\HomeBaseController;
use think\Db;
use think\db\exception\DataNotFoundException;
use think\db\exception\ModelNotFoundException;
use think\Exception;
use think\exception\DbException;
use think\exception\PDOException;

class IndexController extends HomeBaseController
{
    /**
     *
     * @return mixed|string
     */
    public function index()
    {
        $usr = cmf_get_current_admin_id();
        $data = Db::name('oa_tmp')->where(['user' => $usr])->select()->toArray();
        foreach ($data as $k => &$v) {
            $v['lname'] = Db::name('engine')->where(['id' => $v['eid']])->value('name');
        }
        $this->assign('data', $data);
        return $this->fetch();
    }

    /**
     * 添加oa流程
     */
    public function add()
    {
        $data = Db::name('engine')->field('id,name')->select();
        $this->assign('data', $data);
        return $this->fetch();
    }

    /**
     * 执行新增
     */
    public function doadd()
    {
        $data = $this->request->param();
        $files = request()->file('imgs');
        if (empty($data['desc']) || !$files || empty($data['eid'])) {
            $this->error('缺少参数');
        }
        if ($files) {
            foreach ($files as $k => $file) {
                // 移动到框架应用根目录/public/uploads/ 目录下
                $info = $file->move(CMF_ROOT . 'public' . '/' . 'uploads');
                if ($info) {
                    $kps[$k] = '/uploads/' . date('Ymd', time()) . '/' . $info->getFilename();
                } else {
                    $this->error($file->getError());
                }
            }
            $data['imgs'] = implode(',', $kps);
        }
        $data['date'] = date('Y-m-d H:i:s', time());
        $data['user'] = cmf_get_current_admin_id();
        $res = Db::name('oa_tmp')->insert($data);
        if (!$res) {
            $this->error('操作失败');
        }
        $this->success('操作成功');
    }

    /**
     * 查看详情
     * @return mixed|string
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function see()
    {
        $data = $this->request->param();
        $res = Db::name('oa_tmp')->where(['id' => $data['id']])->find();
        $res['imgs'] = explode(',', $res['imgs']);
        foreach ($res['imgs'] as $k => &$v) {
            $v = 'http://' . $_SERVER['SERVER_NAME'] . $v;
        }
        $res['lname'] = Db::name('engine')->where(['id' => $res['eid']])->value('name');
        $setd = Db::name('engine')->field('id,name')->select();
        $this->assign('setd', $setd);
        $this->assign('res', $res);
        return $this->fetch();
    }

    /**
     * 发起流程
     */
    public function send()
    {
        $data = $this->request->param();
        $check = Db::name('oa_tmp')->where(['id' => $data['id']])->find();
        $id = $check['id'];
        unset($check['id'], $check['send']);
        Db::startTrans();
        try {
            Db::name('oa_tmp')->where(['id' => $id])->update(['send' => 1]);
            Db::name('oa_local')->insert($check);
        } catch (\Exception $e) {
            Db::rollback();
            var_dump($e->getMessage());
            $this->error('操作失败');
        }
        Db::commit();
        $this->success('操作成功');
    }

    /**
     * 删除
     * @throws Exception
     * @throws DataNotFoundException
     * @throws ModelNotFoundException
     * @throws DbException
     * @throws PDOException
     */
    public function del()
    {
        $data = $this->request->param();
        if (empty($data['id'])) {
            $this->error('缺少关键参数');
        }
        $check = Db::name('oa_tmp')->where(['id' => $data['id']])->find();
        $res = Db::name('oa_tmp')->where(['id' => $data['id']])->delete();
        if ($check['send'] == 0) {
            if ($res) {
                $check['imgs'] = explode(',', $check['imgs']);
                foreach ($check['imgs'] as $k => &$v) {
                    $v = CMF_ROOT . 'public' . $v;
                    if (file_exists($v) && $v != CMF_ROOT) {
                        unlink($v);
                    }
                }
            }
        }
        if (!$res) {
            $this->error('删除失败');
        }
        $this->success('删除成功');
    }

    public function doedit()
    {
        $data = $this->request->param();
        if (empty($data['desc'])) {
            $this->error('请填写描述');
        }
        $id = $data['id'];
        unset($data['id']);
        $files = $this->request->file('imgs');
        if ($files) {
            $check = Db::name('oa_tmp')->where('id', $id)->value('imgs');
            $cp = explode(',', $check);
            foreach ($cp as $v) {
                $path = CMF_ROOT . 'public' . '/' . $v;
                if (file_exists($path)) {
                    unlink($path);
                }
            }
            $kps = [];
            foreach ($files as $k => $file) {
                // 移动到框架应用根目录/public/uploads/ 目录下
                $info = $file->move(CMF_ROOT . 'public' . '/' . 'uploads');
                if ($info) {
                    $kps[$k] = '/uploads/' . date('Ymd', time()) . '/' . $info->getFilename();
                } else {
                    $this->error($file->getError());
                }
            }
            $data['imgs'] = implode(',', $kps);
        } else {
            unset($data['image']);
        }
        $res = Db::name('oa_tmp')->where(['id' => $id])->update($data);
        if (!$res) {
            $this->error('操作失败');
        }
        $this->success('操作成功');
    }

}